package Aspect;

import POJO.Berth;
import POJO.InputPOJO.BerthBindInput;
import POJO.InputPOJO.BerthInput;
import POJO.Model.PageResponseModel;
import POJO.Model.ResponseModel;
import POJO.Vo.BerthVo;
import Service.BerthService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Aspect
@Component
public class BerthAspect {
    @Resource
    ResponseModel responseModel;
    @Resource
    PageResponseModel pageResponseModel;
    @Resource
    BerthService berthService;

    /*这个通知是添加床位时，做输入检查的。输入参数不全和该病床已经存在均会导致添加失败*/
    @Around(value = "bean(BerthController) &&execution(* Controller.BerthController.addBerth(..))&& args(berth)", argNames ="proceedingJoinPoint,berth")
    public ResponseModel berthInputCheck(ProceedingJoinPoint proceedingJoinPoint,Berth berth) throws  Throwable {
        responseModel.setNull();
        boolean flagDepartmentnull =(berth.getDepartment()==null);
        boolean flagbedNonull =(berth.getDepartment()==null);
        boolean flagBerthNotRepeat=(berthService.selectBerthByBerth(berth)!=null);

        if (flagDepartmentnull) {
            responseModel.setCode(201);
            responseModel.setMsg("插入失败,请填写科室");
            responseModel.setData(null);

        }
        else if(flagbedNonull){
            responseModel.setCode(202);
            responseModel.setMsg("插入失败,请填写床号");
            responseModel.setData(null);

        }
        else if (flagBerthNotRepeat){
            responseModel.setCode(203);
            responseModel.setMsg("插入失败,重复添加");
            responseModel.setData(null);

        }
        else {
            proceedingJoinPoint.proceed(new  Object[]{berth});
            responseModel.setCode(200);
            responseModel.setMsg("插入成功");
            responseModel.setData(null);
        }
        return responseModel;
    }


    /*(在这里没有实现什么功能，留作后续扩展)*/
    @Around(value = "bean(BerthController) &&execution(* Controller.BerthController.queryBerth(..))&& args(berthInput)", argNames ="proceedingJoinPoint,berthInput")
    public PageResponseModel berthInputCheck(ProceedingJoinPoint proceedingJoinPoint, BerthInput berthInput) throws  Throwable {
        proceedingJoinPoint.proceed(new Object[]{berthInput});
        return pageResponseModel;
    }


    /*这个通知用作检验输入的病床是否存在，如果存在就删除，如果不存在就返回病床不存在*/
    @Around(value = "bean(BerthController) && execution(* Controller.BerthController.deleteBerth(..)) && args(berthId)",argNames = "proceedingJoinPoint,berthId")
    public ResponseModel deleteBerthCheck(ProceedingJoinPoint proceedingJoinPoint,int berthId) throws  Throwable
    {
        responseModel.setNull();
        boolean berthNotExist =  berthService.selectBerthById(berthId)==null ;
        if (berthNotExist)
        {
            responseModel.setCode(204);
            responseModel.setMsg("病床不存在");
            responseModel.setData(null);
        }
        else {
            proceedingJoinPoint.proceed(new Object[]{berthId});
            responseModel.setCode(200);
            responseModel.setMsg("删除成功");
            responseModel.setData(null);
        }
        return responseModel;
    }


    /*这个通知是对床位绑定的输入控制*/
    @Around(value = "bean(BerthController) && execution(* Controller.BerthController.berthBind(..)) && args(berthBindInput)",argNames = "proceedingJoinPoint,berthBindInput")
    public ResponseModel BerthBind(ProceedingJoinPoint proceedingJoinPoint, BerthBindInput berthBindInput) throws Throwable
    {   responseModel.setNull();
        BerthVo berthVo=berthService.selectBerthById(berthBindInput.getBerthId());
        if(berthVo==null)
        {
            responseModel.setCode(208);
            responseModel.setMsg("床位不存在");
        }
        else
        {   boolean flagDeviceIsEmpty=berthVo.getPcDeviceMac()==null;
            boolean flagInputDeviceNull=berthBindInput.getPcDeviceMAC()==null;
            if (flagDeviceIsEmpty & !flagInputDeviceNull) {
                proceedingJoinPoint.proceed(new Object[]{berthBindInput});
                responseModel.setCode(200);
                responseModel.setMsg("绑定成功");
            }
            if (flagDeviceIsEmpty & flagInputDeviceNull) {
                responseModel.setCode(207);
                responseModel.setMsg("未绑定任何设备");
            }
            if (!flagDeviceIsEmpty & flagInputDeviceNull) {
                proceedingJoinPoint.proceed(new Object[]{berthBindInput});
                responseModel.setCode(206);
                responseModel.setMsg("解绑成功");
            }
            if (!flagDeviceIsEmpty& !flagInputDeviceNull)
            {
                responseModel.setCode(205);
                responseModel.setMsg("重复绑定");
            }
        }
        return responseModel;
    }
}




